<?php

namespace App\Http\Controllers\Admin\Personnel;

use App\Http\Controllers\Controller;
use App\Models\AdminAuthModel;
use App\Models\AdminModel;
use App\Models\AdminRoleModel;
use App\Models\log\AdminLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

//职位
class Log extends Controller
{

    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $auth_id = trim($request->input('auth_id', ''));
        $create_start = $request->input('startTime', '');
        $create_end = $request->input('endTime', '');

        $query = AdminLogModel::query()->where('flag', 1);
        
        if($auth_id) {
            $query = $query->where('auth_id', $auth_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query->offset(($page - 1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {

            $res = [
                'id' => $item->id,
                'username' => $item->username,
                'title' => $item->title,
                'created_at' => $item->created_at->toDateTimeString(),
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

}
